<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class CountLikeController extends MessageController {

        const APPID = "wxc585cb6b9c965bcf";
        const SECRET="1a7a7236e761b1f370b92c0737731bcf";
    /**
     * 缓存到文件
     */
    private function cachecount(){

        if (S('cachecount')){
            echo count(S('cachecount'));
        }
        $info = M('countlike_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id,countlike_member_id')->where(array('status'=>'-1'))->group('countlike_member_id')->order('wxopen_template_send_id desc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachecount',$data,7200);

        echo count(S('cachecount'));

    }

    public function delcount(){

        exit;

        //$where['createtime'] = array('lt','2018-06-14');
        $where['status'] = array('neq','-1');
        M('countlike_template_send')->where($where)->delete();

    echo 'ok';

    }

    public function cache2(){

        $ids = S('lingshi');
        var_dump($ids);
        file_put_contents('ids.json',json_encode($ids));

    }


    private function cacherank(){

        $cacherank =  S('taskcountrank');

        if (!empty($cacherank)){
            return $cacherank;
        }

        $rank = M('countlike_member')->field('countlike_member_id,nickname,avatarurl,money,rankname')->order('money desc')->limit(150)->select();

        S('taskcountrank',$rank);
        $cacherank =  S('taskcountrank');

        return $cacherank;

    }

    public function songtoken(){

        $this->gettoken2(self::APPID,self::SECRET);
    }

    public function sendcount1($start,$end){

        $info = S('cachecount');

        $name = self::APPID.'access_token';
        $access_token = S($name);

        $tem = "azvK0L6Y_6o_fcF3RLMRtZMzQmhSAUoGvlGfC-Qdk3g";

        foreach ($info as $key=>$value){

            if ($key>=$start&&$key<=$end){

            $fdata = array(
                'keyword1'=>array(
                    'value'=>'千元大奖已送出',
                    'color'=>'#ff0000',
                ),
                'keyword2'=>array(
                    'value'=>'端午节有奖活动',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'端午有奖活动圆满结束，谢谢朋友们的支持，没拿到奖的朋友们我们下次再约！',
                    'color'=>'#173177',
                ),

            );

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$tem,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword1.DATA'
            );
                $res = $this->sendtemplate2($access_token, $data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                 M('countlike_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                 M('countlike_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

        }
    }



    /**
     * 速算
     * @return int
     */
    public function sendcount(){


        exit();
        $wheret['touser'] = "oQRk347uT_XVrm8JUiAl2HDw5u0o";
        $wheret['status'] = "-1";
        $info = M('countlike_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id,countlike_member_id')->where($wheret)->limit(1)->order('wxopen_template_send_id desc')->select();

//        $info = S('cachecount');
//
//        if (empty($info)){
//            $this->cachecount();
//            $info = S('cachecount');
//        }

        $appid = "wxc585cb6b9c965bcf";
        $secret = "1a7a7236e761b1f370b92c0737731bcf";
        $tem = "azvK0L6Y_6o_fcF3RLMRtZMzQmhSAUoGvlGfC-Qdk3g";

        $res = $this->updatetoken($appid,$secret);
        var_dump($res);

        exit();
        foreach ($info as $key=>$value){



            $fdata = array(
                'keyword1'=>array(
                    'value'=>'千元大奖已送出',
                    'color'=>'#ff0000',
                ),
                'keyword2'=>array(
                    'value'=>'端午节有奖活动',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'端午有奖活动圆满结束，谢谢朋友们的支持，没拿到奖的朋友们我们下次再约！',
                    'color'=>'#173177',
                ),

            );

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$tem,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword1.DATA'
            );
            $res = $this->sendtemplate($appid,$secret,$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
               // M('countlike_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
               // M('countlike_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }




}